package com.tencreat.mall.controller.admin.im;

import java.util.List;

import com.tencreat.common.core.HttpResult;
import com.tencreat.domain.im.ImPhrases;
import com.tencreat.service.im.IImPhrasesService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.tencreat.common.annotation.Log;
import com.tencreat.common.core.controller.BaseController;
import com.tencreat.common.core.domain.AjaxResult;
import com.tencreat.common.enums.BusinessType;
import com.tencreat.common.utils.poi.ExcelUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;

/**
 * 常用语Controller
 *
 * @author tencreat
 * @company 深圳腾创互联科技有限公司
 * @link https://tencreat.com
 * @since 2023-11-22
 */
@Api(tags = "IM  常用语api")
@RestController
@RequestMapping("/mall/im/phrases")
@RequiredArgsConstructor
public class ImPhrasesController extends BaseController {

    private final IImPhrasesService imPhrasesService;

    @ApiOperation(value = "查询常用语列表")
    @PostMapping("/list")
        HttpResult<IPage<ImPhrases>> list(@RequestBody ImPhrases imPhrases) {
        IPage<ImPhrases> iPage = generatePage(imPhrases);
        return HttpResult.ok(imPhrasesService.page(iPage, imPhrases));
    }
    
    @ApiOperation(value = "导出常用语列表")
    @Log(title = "常用语", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    AjaxResult export(@RequestBody ImPhrases imPhrases) {
        List<ImPhrases> list = imPhrasesService.page(getMaxPage(), imPhrases).getRecords();
        ExcelUtil<ImPhrases> util = new ExcelUtil<>(ImPhrases. class);
        return util.exportExcel(list, "常用语");
    }

    @ApiOperation(value = "获取常用语详细信息")
    @GetMapping(value = "/getInfo/{id}")
    HttpResult<ImPhrases> getInfo(@PathVariable("id") Long id) {
        return HttpResult.ok(imPhrasesService.getById(id));
    }

    @ApiOperation(value = "新增常用语")
    @Log(title = "常用语", businessType = BusinessType.INSERT)
    @PostMapping(value = "/add")
    HttpResult<Boolean> add(@RequestBody @Validated ImPhrases imPhrases) {
        return to(imPhrasesService.save(imPhrases));
    }

    @ApiOperation(value = "修改常用语")
    @Log(title = "常用语", businessType = BusinessType.UPDATE)
    @PostMapping(value = "/edit")
    HttpResult<Boolean> edit(@RequestBody @Validated ImPhrases imPhrases) {
        return to(imPhrasesService.updateById(imPhrases));
    }

    @ApiOperation(value = "删除常用语")
    @Log(title = "常用语", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    HttpResult<Boolean> remove(@RequestBody List<Long> ids) {
        return to(imPhrasesService.removeByIds(ids));
    }
}